import java.util.StringTokenizer;

class P111_39643 {

    public static void main(String[] args) {
        int n = Integer.parseInt(readLn());
        
        int[] events = new int[n];
        
        StringTokenizer st = new StringTokenizer(readLn());
        
        for (int k = 0; k < n; k++) {
        	events[k] = Integer.parseInt(st.nextToken());
        }

        events = order(events);
        
        String line = readLn();
        
        while (line != null) {
            System.out.println(process(line, events));
            line = readLn();
        }
    }


    private static int[] order(int[] events) {
		int[] eventsOrdered = new int[events.length];
		
//		for (int k = 0; k < events.length; k++) {
//			System.out.print(events[k] + " ");
//		}
//		System.out.println();
		
		for (int k = 0; k < events.length; k++) {
			eventsOrdered[events[k]-1] = k+1;
		}
		
//		for (int k = 0; k < events.length; k++) {
//			System.out.print(eventsOrdered[k] + " ");
//		}
//		System.out.println();
//		
		
		return eventsOrdered;
	}


	private static int process(String line, int[] events) {
        StringTokenizer st = new StringTokenizer(line);
        int[] studentEvents = new int[st.countTokens()];
        for (int k = 0; k < studentEvents.length; k++) {
            studentEvents[k] = Integer.parseInt(st.nextToken());
        }
        
        studentEvents = order(studentEvents);
        
        int x = studentEvents.length + 1;
        int y = events.length + 1;
        
        int[][] counter = new int[x][y];
        
        for (int k = 0; k < x; k++) {
            counter[k][0] = 0;
        }
        
        for (int k = 0; k < y; k++) {
            counter[0][k] = 0;
        }
        
        for (int k = 1; k < y; k++) {
            for (int j = 1; j < x; j++) {
                counter[j][k] = maxOf(counter[j][k-1], counter[j-1][k]);
                if (studentEvents[j-1] == (events[k-1])) {
                    counter[j][k] = counter[j-1][k-1]+1;
                }
            }
        }
        
        x = studentEvents.length;
        y = events.length;
        
//        int cursor = counter[x][y];
        
//        String answerStr = "";
        
//        while (cursor > 0) {
//            if (cursor == counter[x-1][y]) {
//                cursor = counter[x-1][y];
//                x = x -1;
//            } else if (cursor == counter[x][y-1]){
//                cursor = counter[x][y-1];
//                y = y - 1;
//            } else {
//                cursor = counter[x-1][y-1];
//                x = x - 1;
//                y = y - 1;
//                answerStr = studentEvents[x] + " " + answerStr;
//            }
//        }
//        
//        System.out.println(answerStr);
        return counter[studentEvents.length][events.length];
    }

    private static int maxOf(int i, int j) {
        return  i > j ? i : j;
    }


    static String readLn() {
        String newLine = System.getProperty("line.separator");
        StringBuffer buffer = new StringBuffer();
        int car = -1;
        try {
            car = System.in.read();
            while ((car > 0) && (car != newLine.charAt(0))) {
                buffer.append((char)car);
                car = System.in.read();
            }
            if (car == newLine.charAt(0))
            System.in.skip(newLine.length() - 1);
        } catch (java.io.IOException e) { return (null);}
        if ((car < 0) && (buffer.length() == 0)) return (null);
        return (buffer.toString());
    }

    private static boolean isNumber(int car) {
        int carMin = (int) car;
        if ((carMin >= 48) && (carMin <= 57)) {
            return true;
        }
        return false;
    }

    
    private static boolean isLetter(int car) {
        char c = Character.toLowerCase((char)car);
        int carMin = (int)c;
        if ((carMin >= 97) && (carMin <= 122)) {
            return true;
        }
        return false;
    }
 
    
}